package org.nw.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * http请求工具类
 * 
 * @author xuqc
 * @date 2012-8-2 下午02:35:13
 */
public class HttpUtils {

	/**
	 * 发送一个http请求到url，并返回值
	 * 
	 * @param url
	 * @return
	 * @author xuqc
	 * @date 2012-8-2
	 * 
	 */
	public static String request(String url) throws Exception {
		HttpURLConnection conn = null;
		String result = null; // 从
		try {
			URL callUrl = new URL(url);
			conn = (HttpURLConnection) callUrl.openConnection();
			conn.setRequestMethod("GET");
			conn.connect();

			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			java.lang.StringBuffer stringBuffer = new java.lang.StringBuffer();

			String lines = null;
			while((lines = reader.readLine()) != null) {
				stringBuffer.append(lines);
			}

			reader.close();
			result = stringBuffer.toString();
		} finally {
			if(conn != null) {
				conn.disconnect();
			}
		}
		return result;
	}
}
